package yxy.game.pm2.command.capturestrategicpoints;

import org.redisson.api.RLock;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.stargazing;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CaptureStrategicPointsMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsModule;

import java.util.concurrent.TimeUnit;

/**
 * @author wrs
 * @descrition 斩关夺隘观星升级
 * @since 2022-08-17
 */
@MessageType(proto = Command.CaptureStrategicPointsStarsUpgrade, classtype = CaptureStrategicPointsMessage.CaptureStrategicPointsStarsUpgradeRequest.class)
public class CMD_CaptureStrategicPointsStarsUpgrade extends Command<CaptureStrategicPointsMessage.CaptureStrategicPointsStarsUpgradeRequest> {

    @Override
    public void execute(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsStarsUpgradeRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        CaptureStrategicPointsMessage.CaptureStrategicPointsStarsUpgradeResponse.Builder response = CaptureStrategicPointsMessage.CaptureStrategicPointsStarsUpgradeResponse.newBuilder();
        RLock lock = CaptureStrategicPointsModule.getRedisLock(Hero.lockKey(playerCode));

        try {
            CaptureStrategicPointsModule module = CaptureStrategicPointsModule.get(playerCode);

            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            int starsLevel = module.getStarsLevel();//等级
            int totalStars = module.getTotalStars();//剩余星星数量

            stargazing data = DataCenter.getData(starsLevel + 1, stargazing.class);
            if (data == null) {
                send(messageSource, response, Result.CaptureStrategicPointsStarLevelMax);//满级不能再升
                return;
            }

            int needstar = data.getNeedstar();
            if (totalStars < needstar) {
                send(messageSource, response, Result.CaptureStrategicPointsStarNotEnough);//星星不足
                return;
            }

            //todo  业务处理(升级逻辑)
            if (lock.tryLock(5, 10, TimeUnit.SECONDS)) {

                //判断消耗
                Currency cost = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum());
                if (cost != null) {
                    try {
                        new CurrencyHandle().cost(playerCode, CCReason.斩关夺隘, cost);
                    } catch (CurrencyNotEnoughException e) {
                        send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));//消费不足
                        return;
                    }
                }

                //升级
                module.setTotalStars(totalStars - needstar);
                module.setStarsLevel(starsLevel + 1);
                module.set();

                //todo 返回
                response.setDataId(module.getStarsLevel());
                response.setTotalStar(module.getTotalStars());
            }
            send(messageSource, response, Result.Success);

//            PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
//            PlayTypeHandle.saveCaptureStrategicPoints(playerCode, playTypeModule);//提前把战力算到中间缓存
//            new BattleHandle().powerUpdate(playerCode);// 战力更新
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }

    }

    private void send(MessageSource messageSource, CaptureStrategicPointsMessage.CaptureStrategicPointsStarsUpgradeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
